﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Data;
using LotteryVote.Entity;

namespace LotteryVote.Logic
{
    public class CompanyFacade
    {
        private LotteryCompanyDataAccess _daCompany;
        public LotteryCompanyDataAccess DACompany
        {
            get
            {
                if (_daCompany == null)
                    _daCompany = new LotteryCompanyDataAccess();
                return _daCompany;
            }
            set
            {
                _daCompany = value;
            }
        }

        /// <summary>
        /// 添加公司.
        /// </summary>
        /// <param name="name">公司名称.</param>
        /// <param name="abbreviation">公司名称简称.</param>
        /// <param name="typeId">公司类型Id.</param>
        /// <param name="region">公司所属区域.</param>
        /// <param name="openTime">开盘时间.</param>
        /// <param name="closeTime">关盘时间.</param>
        public void Add(string name, string abbreviation, int typeId, Region region, TimeSpan openTime, TimeSpan closeTime)
        {
            LotteryCompany company = new LotteryCompany
            {
                Name = name,
                Abbreviation = abbreviation,
                Region = region,
                OpenTime = openTime,
                CloseTime = closeTime,
                CompanyType = new CompanyTypeModel
                {
                    Id = typeId
                }
            };
            DACompany.Insert(company);
        }
        public void Remove(int companyId)
        {
            DACompany.Delete(companyId);
        }
        public void Remove(string companyName)
        {
            if (string.IsNullOrEmpty(companyName))
                throw new ArgumentNullException("CompanyName");
            DACompany.Delete(companyName);
        }
        public void Update(int companyId, string name, string abbreviation, int typeId, Region region, TimeSpan openTime, TimeSpan closeTime)
        {
            LotteryCompany company = new LotteryCompany
            {
                CompanyId = companyId,
                Name = name,
                Abbreviation = abbreviation,
                Region = region,
                OpenTime = openTime,
                CloseTime = closeTime,
                CompanyType = new CompanyTypeModel
                {
                    Id = typeId
                }
            };
            Update(company);
        }
        public void Update(LotteryCompany company)
        {
            DACompany.Update(company);
        }

        /// <summary>
        /// 是否已存在指定公司.
        /// </summary>
        /// <param name="companyName">公司名称.</param>
        /// <returns></returns>
        public bool ExistCompany(string companyName)
        {
            return DACompany.ExistCompany(companyName);
        }

        public LotteryCompany GetCompany(int companyId)
        {
            return DACompany.GetCompany(companyId);
        }
        public IList<LotteryCompany> GetAllCompany()
        {
            return DACompany.GetAllCompany();
        }
    }
}
